package com.gitee.elf.api.nms

import com.gitee.elf.api.annotation.EntityId
import com.gitee.elf.api.annotation.Id
import org.bukkit.Location
import org.bukkit.entity.Entity
import org.bukkit.entity.EntityType
import taboolib.common.LifeCycle
import taboolib.common.io.runningClasses
import taboolib.common.platform.Awake
import taboolib.common.platform.function.info
import taboolib.library.reflex.Reflex.Companion.invokeConstructor
import taboolib.module.nms.MinecraftVersion
import taboolib.module.nms.MinecraftVersion.minecraftVersion

interface EntityTypeManager {

    val types: MutableMap<String, Class<NMSEntity>>

    @Suppress("UNCHECKED_CAST")
    fun registerNMSEntities() {
        runningClasses.forEach {
            if (NMSEntity::class.java.isAssignableFrom(it) && it.name.contains(minecraftVersion) && it.isAnnotationPresent(EntityId::class.java)) {
                val annotation = it.getAnnotation(EntityId::class.java)
                val nmsEntityClass = it as Class<NMSEntity>
                types[annotation.value.lowercase()] = nmsEntityClass
                info("Loaded ${it.name} entity type")
                registerEntity(annotation.value,annotation.bind,nmsEntityClass)
            }
        }
    }

    fun create(location: Location,instance: Any, type: String): Entity?

    fun registerEntity(id: String,bind: EntityType,entity: Class<NMSEntity>)

    companion object {

        val INSTANCE = nmsEntityTypeManager().invokeConstructor()

        @Awake(LifeCycle.LOAD)
        fun registerNMSEntities() {
            info("NMS scan driver ${INSTANCE::class.java.name}")
            INSTANCE.registerNMSEntities()
        }

    }

}